package com.cashoutgaming.client.modules.home;

import com.cashoutgaming.client.common.images.CommonToolImages;
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewWithUiHandlers;
import com.sencha.gxt.core.client.XTemplates;
import com.sencha.gxt.core.client.util.IconHelper;
import com.sencha.gxt.widget.core.client.TabItemConfig;
import com.sencha.gxt.widget.core.client.TabPanel;
import com.sencha.gxt.widget.core.client.container.BorderLayoutContainer;
import com.sencha.gxt.widget.core.client.container.BorderLayoutContainer.BorderLayoutData;
import com.sencha.gxt.widget.core.client.container.CenterLayoutContainer;
import com.sencha.gxt.widget.core.client.container.SimpleContainer;

/**
 * This is the admin tool desktop view. There are presently no requirements to
 * have any other form factor views for the admin tool, so this will be the only
 * one
 */
public class HomeView extends ViewWithUiHandlers<HomeUiHandlers> implements HomePresenter.MyView {

	private BorderLayoutContainer blContainer;

	private HomeStyle style;
	private TabPanel tabPanel;
	private SimpleContainer recentMatchContainer;

	interface HomeStyle extends CssResource {

		String container();
	}

	interface StyleResources extends ClientBundle {

		@Source("HomeCss.css")
		HomeStyle css();
	}

	interface DesktopShellCssTemplate extends XTemplates {

		@XTemplate(source = "DesktopShellCssTemplate.txt")
		SafeHtml generateCustomCss(String mainBgColor, String mainBgImage);
	}

	@Inject
	public HomeView() {
		blContainer = new BorderLayoutContainer();
		final StyleResources resources = GWT.create(StyleResources.class);
		resources.css().ensureInjected();
		style = resources.css();

		blContainer.setStylePrimaryName(style.container());

		String logo = IconHelper.getHTML(CommonToolImages.INSTANCE.getLogoLarge());
		// String logo = "";
		HTML poweredBy = new HTML(logo);
		// poweredBy.setStyleName(style.footerMargin());
		CenterLayoutContainer footer = new CenterLayoutContainer();
		footer.add(poweredBy);

		BorderLayoutData southData = new BorderLayoutData();
		southData.setCollapsible(false);
		southData.setSplit(false);
		southData.setSize(300);
		blContainer.setNorthWidget(footer, southData);

		tabPanel = new TabPanel();
		tabPanel.setPixelSize(600, 350);
		tabPanel.setAnimScroll(true);
		tabPanel.setTabScroll(true);
		tabPanel.setCloseContextMenu(true);

		TabItemConfig dashtic = new TabItemConfig("Recent Matches");
		recentMatchContainer = new SimpleContainer();
		tabPanel.add(recentMatchContainer, dashtic);

		CenterLayoutContainer tabContainer = new CenterLayoutContainer();
		tabContainer.add(tabPanel);

		BorderLayoutData centerData = new BorderLayoutData();
		southData.setCollapsible(false);
		southData.setSplit(false);
		southData.setSize(300);
		blContainer.setSouthWidget(tabContainer, centerData);

		this.initWidget(blContainer);
	}

	@Override
	public void setInSlot(Object slot, IsWidget content) {
		if (slot == HomePresenter.TYPE_TWITCH) {
			SimpleContainer simple = new SimpleContainer();
			simple.setBorders(false);
			simple.setWidget(content);
			// simple.getWidget().setStylePrimaryName(style.window());
			blContainer.setCenterWidget(simple);
		}
		else if (slot == HomePresenter.TYPE_RECENT_MATCHES) {
			recentMatchContainer.add(content);
			tabPanel.forceLayout();
		}
		blContainer.forceLayout();
	}
}
